<?php


class Kro extends Controller 
{

	private $kro = null;
	public $sheet_num = 0;

	function index()
	{
		global $USER_LEVEL_BONUS_TXT, $TYPE_OPER;
		$users = $this->MUser->getChildrenAndMe(Auth::$user_id);

		$me = $this->MUser->get(Auth::$user_id);

		$users_email_index = array();
		$allreferer_emails = array();
		$all_operations_id = array();
		foreach($users as $k => $user)
		{
			$filters = array('user_id' => $user['id'], 'type' => 'buy');
			$operations = $this->MOper->getList($filters);
			$all_operations_id = array_merge($all_operations_id, array_keys($operations));
			$users_email_index[$user['email']] = $user;
			$allreferer_emails[$user['referer_email']] = $user['referer_email'];
		}

		$filters_operations = array('type' => array('ref', 'buy', 'sell', 'sell.mass', 'modify.buy', 'modify.sell', 'ng', 'patriot', 'arest', '8marta', 'reg', 'week', 'l*', 'sell.fire.ng', 'sell.fire.patriot', 'sell.fire.patriot', 'sell.fire.arest', 'sell.fire.8marta', 'sell.fire.ref', 'sell.fire.l*'));

		$this->_createKro();
		$this->kro->createBook();

		$this->_createBalanceSheet('Баланс', &$users, $filters_operations);

		$cur_level = 1;
		$filters_operations2 = array();
		$users_me = array($me['id'] => $me);
		foreach($TYPE_OPER as $k => $v)
		{
			if(substr($k, 0, 2) != 'l*')
			{
				continue;
			}
			$l = str_replace("l", "", $k);
			$filters_operations2 = array('type' => array($k, 'sell.fire.'.$k));
			if(strlen($l) <= strlen($me['level']) || $this->MOper->getCount(array('user_id' => $me['id'], 'type' => $k)))
			{
				$this->_createBalanceSheet('Бонусы '.$USER_LEVEL_BONUS_TXT[$l], $users_me, $filters_operations2);
			}
			else
			{
				break;
			}
		}

		$email_referers = array();
		foreach($allreferer_emails as $r_email)
		{
			if(isset($users_email_index[$r_email]))
			{
				continue;
			}
			$email_referers[] = $r_email;
		}
		$filters_referers = count($email_referers) ? array('email' => $email_referers) : array('id' => -1);
		$users_referers = $this->MUser->getList($filters_referers);
		$all_operations_id = count($all_operations_id) ? $all_operations_id : array(-1);
		$filters_referer_operations = array('type' => 'ref', 'parent_id' => $all_operations_id);
		$this->_createBalanceSheet('Внешние рефереры', $users_referers, $filters_referer_operations, true);

		$this->kro->closeBook();
		$this->_download();
	}

	function user($user_id = null)
	{
		if(!$this->MUser->inMyStructure($user_id))
		{
			return;
		}
		$user = $this->MUser->get($user_id);
		$users = array($user['id'] => $user);

		$this->_createKro();
		$this->kro->createBook($user['fio']);
		$this->_createBalanceSheet('Баланс', &$users);
		$this->kro->closeBook();
		$this->_download();
	}

	function me()
	{
		$this->user(Auth::$user_id);
	}

	private function _createKro()
	{
		$this->load->library('KroExcel');
		$this->load->model('MSetting');

		$setting = $this->MSetting->getMe();
		if($setting['format_kro'] == '2003') $this->kro = new KroExcel_2003;
		elseif($setting['format_kro'] == '2007') $this->kro = new KroExcel_2007;
		else $this->kro = new KroExcel_98;
	}

	private function _createBalanceSheet($sheet_name, $users, $filters_operations = array(), $add_ref_fire = false)
	{
		if(count($users))
		{
			$filters_operations['user_id'] = array_keys($users);
			if(!isset($filters_operations['sort']))
			{
				$filters_operations['sort'] = 'date asc,id asc';
			}
			$all_opers = $this->MOper->getList($filters_operations);
			if($add_ref_fire)
			{
				$filters_fire = array(
					'parent_id' => count($all_opers) ? array_keys($all_opers) : -1, 
					'type' => 'sell.fire.ref'
				);
				$fire_operations = $this->MOper->getList($filters_fire);
				foreach($fire_operations as $o)
				{
					$all_opers[$o['id']] = $o;
				}
			}
			foreach($all_opers as $oper)
			{
				if(isset($users[$oper['user_id']]))
				{
					$users[$oper['user_id']]['operations'][$oper['id']] = $oper;
				}
			}

			$all_opers_id = $this->sheet_num > 0 ? array_keys($all_opers) : null;
			$all_mavro_summ = $this->MOper->getMavroSumm(array_keys($users), $all_opers_id);
			foreach($all_mavro_summ as $u_id => $valutas)
			{
				$users[$u_id]['all_mavro'] = $valutas;
			}
		}
		$this->kro->createSheetBalance($sheet_name, $this->sheet_num, $users);
		$this->sheet_num++;
	}


	private function _download()
	{
		header("Content-type: application/vnd.ms-excel");
		header("Content-Disposition: attachment; filename=".$this->kro->filename_download);
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Pragma: public");
		$output = file_get_contents($this->kro->filename);
		@unlink($this->kro->filename);
		die($output);
	}



}


